package me.fung.rem.app.main.version

import me.fung.rem.bean.database
import me.fung.rem.bean.repo.configVersion
import mu.KLogger
import mu.KotlinLogging
import org.ktorm.database.Database
import java.sql.Connection

abstract class VersionUpdater {
    abstract val targetVersion: Float
    private val logger: KLogger = KotlinLogging.logger {}
    fun update() {
        if (configVersion >= targetVersion) return // 已升级
        database.useTransaction {
            database.useConnection { connection: Connection ->
                updateSql(database, connection)
            }
            configVersion = targetVersion
            logger.info { "update to database version:${targetVersion} success" }
        }
    }

    abstract fun updateSql(db: Database, connection: Connection)

    fun String.createStatementAndExecute(c: Connection) {
        c.createStatement().execute(this)
    }
}